/*
* @Author: dyiwen
* @Date:   2020-09-21 14:14:30
* @Last Modified by:   dyiwen
* @Last Modified time: 2020-09-22 16:00:50
 */

package main

import (
	"crypto/tls"
	"encoding/json"
	"fmt"
	"github.com/kirinlabs/HttpRequest"
	"io/ioutil"
	"net/http"
	"net/url"
	"os"
)

//编码url
func urlCodeHandler(param string) string {
	return url.QueryEscape(url.QueryEscape(param))
}

//关闭证书认证
func request(skip_https bool) (req *HttpRequest.Request) {
	if skip_https {
		tr := &http.Transport{
			TLSClientConfig: &tls.Config{InsecureSkipVerify: true},
		}
		req = HttpRequest.NewRequest().Transport(tr)
	} else {
		req = HttpRequest.NewRequest()
	}
	return
}

// 字节的单位转换 保留两位小数
func formatFileSize(fileSize int64) (size string) {
	if fileSize < 1024 {
		//return strconv.FormatInt(fileSize, 10) + "B"
		return fmt.Sprintf("%.2fB", float64(fileSize)/float64(1))
	} else if fileSize < (1024 * 1024) {
		return fmt.Sprintf("%.2fKB", float64(fileSize)/float64(1024))
	} else if fileSize < (1024 * 1024 * 1024) {
		return fmt.Sprintf("%.2fMB", float64(fileSize)/float64(1024*1024))
	} else if fileSize < (1024 * 1024 * 1024 * 1024) {
		return fmt.Sprintf("%.2fGB", float64(fileSize)/float64(1024*1024*1024))
	} else if fileSize < (1024 * 1024 * 1024 * 1024 * 1024) {
		return fmt.Sprintf("%.2fTB", float64(fileSize)/float64(1024*1024*1024*1024))
	} else { //if fileSize < (1024 * 1024 * 1024 * 1024 * 1024 * 1024)
		return fmt.Sprintf("%.2fEB", float64(fileSize)/float64(1024*1024*1024*1024*1024))
	}
}

// 生成配置文件
func write_to_config(c Config) (err error) {
	b, err := json.Marshal(c)
	if err != nil {
		return err
	}
	err = CreatePath(b, path)
	if err != nil {
		return err
	}
	return
}

/*
不存在则创建文件
*/
func CreatePath(content []byte, path string) (err error) {
	// ok := Exists(path)
	// if !ok {
	// err = os.Create(path)
	// }
	// return
	MkdirPath(homedir)
	f, err := os.Create(path)
	defer f.Close()
	if err != nil {
		return err
	}
	_, err = f.Write(content)
	return
}

/*
不存在则创建目录
*/
func MkdirPath(path string) (err error) {
	if !Exists(path) {
		err = os.MkdirAll(path, os.ModePerm)
	}
	return
}

// 判断所给路径文件/文件夹是否存在
func Exists(path string) bool {
	_, err := os.Stat(path) //os.Stat获取文件信息
	if err != nil {
		if os.IsExist(err) {
			return true
		}
		return false
	}
	return true
}

func RemoveAll(path string) (err error) {
	if Exists(path) {
		err = os.RemoveAll(path)
	}
	return err
}

// 判断所给路径是否为文件夹
func IsDir(path string) bool {
	s, err := os.Stat(path)
	if err != nil {
		return false
	}
	return s.IsDir()
}

// json文件转struct
func file_to_struct() (err error) {
	content, err := file_get_contents(path)
	if err != nil {
		return err
	}
	err = json.Unmarshal([]byte(content), &conf)
	if err != nil {
		return err
	}
	return err
}

// 读取文件内容
func file_get_contents(path string) ([]byte, error) {
	if !Exists(path) {
		fmt.Println("请先执行harborctl login!")
		os.Exit(1)
	}
	f, err := os.Open(path)
	defer f.Close()
	if err != nil {
		fmt.Println("请先执行harborctl login!")
		return nil, err
	}
	return ioutil.ReadAll(f)
}

/*
获取环境变量
*/
func GetEnv(param string) string {
	return os.Getenv(param)
}
